/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.shef.zeno.exercise;

import com.primesense.nite.NiTE;
import com.primesense.nite.UserTracker;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.openni.Device;
import org.openni.DeviceInfo;
import org.openni.OpenNI;

import uk.ac.shef.zeno.utils.ReadConfig;

/**
 *
 * @author samf
 */
public class ExerciseApplication {
    

    private JFrame mFrame;
    private ExerciseViewer mViewer;
    private boolean mShouldRun = true;
     

    public ExerciseApplication(Device device, UserTracker tracker) {
        HashMap<String, String> configs = ReadConfig.readConfig();
        mFrame = new JFrame("NiTE User Tracker Viewer");
        PositionPanel positionPanel = new PositionPanel();
        
        mViewer = new ExerciseViewer(tracker, positionPanel);
        
        
        
        JPanel panel = new JPanel(new GridLayout(1,2));
        panel.add(mViewer);
        panel.add(positionPanel);

        // register to key events
        mFrame.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent arg0) {}
            
            @Override
            public void keyReleased(KeyEvent arg0) {}
            
            @Override
            public void keyPressed(KeyEvent arg0) {
                if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    mShouldRun = false;
                }
            }
        });
        
        // register to closing event
        mFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                mShouldRun = false;
            }
        });
  
     
        positionPanel.setSize(800,800);

        mViewer.setSize(800, 800);
          mFrame.add(panel);
         mFrame.setSize(1600, 800);
        
        mFrame.setVisible(true);
    }

    void run() {
        while (mShouldRun) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
      
        mFrame.dispose();
        System.exit(0);
    }

    public static void main(String s[]) {
        // initialize OpenNI and NiTE
    	OpenNI.initialize();
        NiTE.initialize();
        
        List<DeviceInfo> devicesInfo = OpenNI.enumerateDevices();
        if (devicesInfo.isEmpty()) {
            JOptionPane.showMessageDialog(null, "No device is connected", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        Device device = Device.open(devicesInfo.get(0).getUri());
        UserTracker tracker = UserTracker.create();
        final ExerciseApplication app = new ExerciseApplication(device, tracker);
        app.run();
    }
}

